<?php

  session_start();
  include "config.php";
  include "ALorder.php";

  date_default_timezone_set("Australia/Sydney");

  if(isset($_SESSION["user"]))
  {
    if(isset($_SESSION["o_products"]))
    {
      include "$header";
      include "$body";
  
?>

<div style="position:absolute; top:0px; left:1px; border:0;">
  <span class="prevPage">Order Products:Search > </span>
  <span class="prevPage">Confirm Customer Details > </span>
  <span class="prevPage">Order Products > </span>
  <span class="page">Confirm Order</span>
</div>
  
<?php

      include "$H_op";

      if(isset($_GET["no"]))
      {
        echo "<SCRIPT language='Javascript'>";
        echo "  window.location.href = 'orderProducts_2orderProducts.php';";
        echo "</SCRIPT>";
      }
      else if(isset($_GET["yes"]))
      {
        if(addOrderDetails($_SESSION["c_no"], $_SESSION["o_products"], $_SESSION["o_freight"], $_SESSION["o_merchanise"], $_SESSION["o_delivery"], $_SESSION["o_insurance"], $_SESSION["o_total"], $_SESSION["o_card"]) == true)
        {
          $_SESSION["orderedSuccess"] = true;
          echo "<SCRIPT language='Javascript'>";
          echo "  window.location.href = 'orderProducts_4success.php';";
          echo "</SCRIPT>";
        }
        else
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  window.location.href = 'orderProducts_2orderProducts.php';";
          echo "</SCRIPT>";
        }
      }
      else
      {
        showForm();
      }

      include "$footer";
    }
    else
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  window.location.href = 'orderProducts.php';";
      echo "</SCRIPT>";
    }
  }
  else
  {
    session_destroy();
    echo "<SCRIPT language='Javascript'>";
    echo "  parent.window.location.href = 'index.php';";
    echo "</SCRIPT>";
  }

  function showForm()
  {
    $productDetails = $_SESSION["o_productDetails"];
    $orderProducts = $_SESSION["o_products"];
    $freight = $_SESSION["o_freight"];
    $merchanise = $_SESSION["o_merchanise"];
    $delivery = $_SESSION["o_delivery"];
    $insurance = $_SESSION["o_insurance"];
    $orderTotal = $_SESSION["o_total"];
    $payment = $_SESSION["o_payment"];
    $card_details = $_SESSION["o_card"];
?>

<p>
<b>CONFIRMATION: ARE THESE DETAILS ACCURATE?</b>
<p align="left">
<b>Date:</b> <?php echo date("Y-m-d"); ?>
</p>

<FORM name="confirmOrder" action="" method="get">
<TABLE class="order_products" cellpadding="2" cellspacing="2">
  <TR>
    <TD class="order_header" style="width:80px">
      Product No
    </TD>
    <TD class="order_header" style="width:60px">
      Quantity
    </TD>
    <TD class="order_header" style="width:80px">
      Colour
    </TD>
    <TD class="order_header" style="width:50px">
      Size
    </TD>
    <TD class="order_header" style="width:150px">
      Description
    </TD>
    <TD class="order_header" style="width:80px">
      Item Price $
      <br /><small>incl. GST</small>
    </TD>
    <TD class="order_header" style="width:80px">
      Total Price $
      <br /><small>incl. GST</small>
    </TD>
  </TR>

<?php

    for($i=0; $i < sizeof($orderProducts); $i+=2)
    {
      for($j=0; $j < sizeof($productDetails); $j++)
      {
        $productInfo = $productDetails[$j];
        if($productInfo[0] == $orderProducts[$i])
        {
          $colour = $productInfo[1];
          $size = $productInfo[2];
          $des = $productInfo[3];
          $item = $productInfo[4];
		  $total = ($item * $orderProducts[$i+1]);
          $total = number_format($total, 2);
          break;
        }
      }

?>

  <TR>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>" style="width:80px" onChange="getDetails(this.name, product, detailsSize); getMerchanisePrice()" value="<?php echo $orderProducts[$i]; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_qty" style="width:60px" onChange="getTotalPrice(this.name, this.value, product, detailsSize); getMerchanisePrice()"  value="<?php echo $orderProducts[$i+1]; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_colour" style="width:90px" value="<?php echo $colour; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_size" style="width:50px" value="<?php echo $size; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_des" style="width:150px"  value="<?php echo $des; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_item" style="width:70px" value="<?php echo $item; ?>" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_total" style="width:70px" value="<?php echo $total; ?>" readonly />
    </TD>
  </TR>

<?php

    }

?>

</TABLE>

<p>

<TABLE style="border:1px solid #000000" cellpadding="8">
  <TR>
    <TD class="order_odHeader" valign="top" align="right">
      Merchanise Total: $
    </TD>
    <TD class="order_odEnterDetails" valign="top">
      <?php echo $merchanise; ?>
    </TD>
  </TR>
  <TR>
    <TD class="order_odHeader" valign="top" align="right">
      Delivery Charge (<?php echo $freight; ?>): $
    </TD>
    <TD class="order_odEnterDetails" valign="top">
      <?php echo $delivery; ?>
    </TD>
  </TR>
  <TR>
    <TD class="order_odHeader" valign="top" align="right">
      Insurance: $
      <br />
      <small>@ $1.00 per $100 value</small>
    </TD>
    <TD class="order_odEnterDetails" valign="top">
      <?php echo $insurance; ?>
    </TD>
  </TR>
  <TR>
    <TD class="order_odHeader" valign="top" align="right">
      TOTAL: $
    </TD>
    <TD class="order_odEnterDetails" valign="top">
      <b><?php echo $orderTotal; ?></b>
    </TD>
  </TR>
</TABLE>

</p>

<TABLE border="0" cellpadding="3">
  <TR>
    <TD align="center">
<?php

    if($payment == "preferred")
    {
      echo "USE <b>CURRENT PREFERRED</b> CREDIT CARD DETAILS:";
    }
    else if($payment == "alternate")
    {
      echo "USE <b>CURRENT ALTERNATE</b> CREDIT CARD DETAILS:";
    }
    else if($payment == "other")
    {
      echo "USE <b>OTHER</b> CREDIT CARD DETAILS:";
    }

?>
    </TD>
  </TR>
  <TR>
    <TD>
      <TABLE style="border:1px solid #000000" cellpadding="5">
        <TR>
          <TD align="right">
            <b>Card Type:</b>
          </TD>
          <TD>
            <?php echo $card_details[0]; ?>
          </TD>
        </TR>
        <TR>
          <TD align="right">
            <b>Card Name:</b>
          </TD>
          <TD>
            <?php echo $card_details[1]; ?>
          </TD>
        </TR>
        <TR>
          <TD align="right">
            <b>Card Number:</b>
          </TD>
          <TD>
            <?php echo $card_details[2] . "-" . $card_details[3] . "-" . $card_details[4] . "-" . $card_details[5]; ?>
          </TD>
        </TR>
        <TR>
          <TD align="right">
            <b>Card Expiry Date:</b>
          </TD>
          <TD>
            <?php echo getMonth($card_details[6]) . " 20" . $card_details[7]; ?>
          </TD>
        </TR>
      </TABLE>
    </TD>
  </TR>
</TABLE>

<p>

<input class="submit_off" type="submit" value="No" name="no" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />
&nbsp;&nbsp;
<input class="submit_off" type="submit" value="Yes" name="yes" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />

</FORM>

<p>
&nbsp;
</p>

</BODY>

<?php

  }

  function getMonth($month)
  {
    if($month == "01")
    {
      return "January";
    }
    else if($month == "02")
    {
      return "February";
    }
    else if($month == "03")
    {
      return "March";
    }
    else if($month == "04")
    {
      return "April";
    }
    else if($month == "05")
    {
      return "May";
    }
    else if($month == "06")
    {
      return "June";
    }
    else if($month == "07")
    {
      return "July";
    }
    else if($month == "08")
    {
      return "August";
    }
    else if($month == "09")
    {
      return "September";
    }
    else if($month == "10")
    {
      return "October";
    }
    else if($month == "11")
    {
      return "November";
    }
    else if($month == "12")
    {
      return "December";
    }
    else
    {
      echo "error in getmonth()";
    }
  }
  
?>